﻿using System;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Tressel_asm
{
    class TresselIntermediate
    {
        /*#
        Variable Name: source
        Module Where Defined: TresselIntermediate
        Data Type: ArrayList
        Local or Global: Global
        Purpose:  holds the original tressel source
        #*/
        private System.Collections.ArrayList source;
        /*#
        Variable Name: currentInstruction
        Module Where Defined: TresselIntermediate
        Data Type: Instruction
        Local or Global: Global
        Purpose:  used to hold the current instruction
        #*/
        private Instruction currentInstruction;
        /*#
        Variable Name: startsBeenFound
        Module Where Defined: TresselIntermediate
        Data Type: bool
        Local or Global: Global
        Purpose:  stores whether or not the START directive has been found
        #*/
        private bool startsBeenFound = false;
        /*#
        Variable Name: instructionList
        Module Where Defined: TresselIntermediate
        Data Type: ArrayList
        Local or Global: Global
        Purpose:  used to house the intermediate code
        #*/
        private System.Collections.ArrayList instructionList;
        /*#
        Variable Name: lineCounter
        Module Where Defined: TresselIntermediate
        Data Type: int
        Local or Global: Global
        Purpose:  keeps track of the line number
        #*/
        private int lineCounter;
        /*#
        Variable Name: locationCounter
        Module Where Defined: TresselIntermediate
        Data Type: int
        Local or Global: Global
        Purpose:  keeps track of the location in memory
        #*/
        private int locationCounter;
        /*#
        Variable Name: startingLocationCounter
        Module Where Defined: TresselIntermediate
        Data Type: int
        Local or Global: Global
        Purpose:  keeps track of the start location in memory
        #*/
        private int startingLocationCounter;
        /*#
        Variable Name: altStartExists
        Module Where Defined: TresselIntermediate
        Data Type: bool
        Local or Global: Global
        Purpose:  keeps track of the altstart existance in program.
        #*/
        private bool altStartExists = false;
        /*#
        Variable Name: instructionDirectivesTable
        Module Where Defined: TresselIntermediate
        Data Type: DataTable
        Local or Global: Global
        Purpose:  holds the instructions and directives in a table object
        #*/
        private DataTable instructionDirectivesTable;
        /*#
        Variable Name: errorTable
        Module Where Defined: TresselIntermediate
        Data Type: DataTable
        Local or Global: Global
        Purpose:  holds all possible errors generated by any tressel source
        #*/
        private DataTable errorTable;
        /*#
        Variable Name: symbolTable
        Module Where Defined: TresselIntermediate
        Data Type: DataTable
        Local or Global: Global
        Purpose:  holds the symbols in a table object
        #*/
        private DataTable symbolTable;
        /*#
        Variable Name: generatedErrorsTable
        Module Where Defined: TresselIntermediate
        Data Type: DataTable
        Local or Global: Global
        Purpose:  holds the errors generated by the tressel source
        #*/
        private DataTable generatedErrorsTable = new DataTable();
        /*#
        Variable Name: fatalErrors
        Module Where Defined: TresselIntermediate
        Data Type: bool
        Local or Global: Global
        Purpose:  keeps track of whether or not a fatal error has occurred
        #*/
        private bool fatalErrors = false;
        /*#
        Variable Name: debugRegisterState
        Module Where Defined: TresselIntermediate
        Data Type: string
        Local or Global: Global
        Purpose:  keeps track of whether or not the debug is set
        #*/
        private string debugRegisterState = "0";
        /*#
        Variable Name: baseRegisterState
        Module Where Defined: TresselIntermediate
        Data Type: string
        Local or Global: Global
        Purpose:  adds public access to baseRegisterState
        #*/
        private string baseRegisterState = "0";
        /*#
        Variable Name: instructionAllowed
        Module Where Defined: TresselIntermediate
        Data Type: bool
        Local or Global: Global
        Purpose:  keeps track of whether an instruction is allowed
        #*/
        private bool instructionAllowed = false;
        /*#
        Variable Name: programEnds
        Module Where Defined: TresselIntermediate
        Data Type: bool
        Local or Global: Global
        Purpose:  set when the END directive is processed
        #*/
        private bool programEnds = false;
        /*#
        Variable Name: codeForm
        Module Where Defined: TresselIntermediate
        Data Type: CodeDisplayForm
        Local or Global: Global
        Purpose:  allows the TresselIntermediate class to access the form which instantiated it
        #*/
        private Tressel_asm.CodeDisplayForm codeForm;
        /*#
        Variable Name: tresselReader
        Module Where Defined: TresselIntermediate
        Data Type: TextReader
        Local or Global: Global
        Purpose:  reads the tressel source
        #*/
        private System.IO.TextReader tresselReader;

        public void InsertInstruction(Instruction iInstruction)
        {
            /*!
            Procedure Name: InsertInstruction
            Description: insert valid instruction to the instruction list.for record of the program.
            Input Parameters: (Instruction) iInstruction
            Output Parameters: None
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Louis
            Procedure Creation Date: 10/16/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            instructionList.Add(iInstruction);
        }

        public int StartingLocationCounter
        {
            /*!
           Procedure Name: StartingLocationCounter
           Description: This simply returns/sets the starting location counter.  The Instruction class
           will need to know the starting location counter when checking "USING"
           Input Parameters: none
           Output Parameters: startingLocationCounter
           Error Conditions Tested:
           Error Messages Generated:
           Original Author: Steve Rudy
           Procedure Creation Date: 11/4/2009
           Modification Log:
           Who:  
           When: 
           Why: 
           Coding Standards Met: SR
           Testing Standards Met: RM
           !*/
            set
            {
                startingLocationCounter = value;
            }
            get
            {
                return startingLocationCounter;
            }
        }
        public bool StartsBeenFound
        {
            /*!
           Procedure Name: StartsBeenFound
           Description: This makes public the startsBeenFound boolean
           Input Parameters: none
           Output Parameters: startsBeenFound
           Error Conditions Tested:
           Error Messages Generated:
           Original Author: Steve Rudy
           Procedure Creation Date: 11/15/2009
           Modification Log:
           Coding Standards Met: SR
           Testing Standards Met: RM
           !*/
            set
            {
                startsBeenFound = value;
            }
            get
            {
                return startsBeenFound;
            }
        }
        public System.Collections.ArrayList Source
        {
            /*!
           Procedure Name: Source
           Description: This makes public the source member of the TresselIntermediate class
           Input Parameters: none
           Output Parameters: source
           Error Conditions Tested:
           Error Messages Generated:
           Original Author: Steve Rudy
           Procedure Creation Date: 11/11/2009
           Modification Log:
           Who:  
           When: 
           Why: 
           Coding Standards Met: SR
           Testing Standards Met: RM
           !*/
            set
            {
                source = value;
            }
            get
            {
                return source;
            }
        }
        public bool AltStartExist
        {
           /*!
           Procedure Name:AltStartExist
           Description: return exitance of altstart
           Input Parameters: none
           Output Parameters: altStartExist
           Error Conditions Tested:
           Error Messages Generated:
           Original Author: Louis Pan
           Procedure Creation Date: 11/7/2009
           Modification Log:
           Coding Standards Met: SR
           Testing Standards Met: RM
           !*/
            set
            {
                altStartExists = value;
            }
            get
            {
                return altStartExists;
            }
        }
        public int LineCounter
        {
            /*!
            Procedure Name: LineCounter
            Description: This simply returns the line counter.  The Instruction class, and other 
            classes of lesser scope will need to know the line counter to make sure, for example,
            tressel input doesn't add the using keyword in the middle of the program
            Input Parameters: none
            Output Parameters: lineCounter
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/12/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/27/2009
            Why: this method needed to be turned into a property
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return lineCounter;
            }
        }

        public DataTable GeneratedErrorTable
        {
            /*!
            Procedure Name: GeneratedErrorTable
            Description: provides access to the generatedErrorTable datatable for the CodeDisplayForm
            Input Parameters: none
            Output Parameters: generatedErrorTable
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 11/2/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return generatedErrorsTable;
            }
        }
        public string DebugRegisterState
        {
            /*!
            Procedure Name: DebugRegisterState
            Description: provides access to the debugRegisterState string for the Instruction class
            Input Parameters: none
            Output Parameters: debugRegisterState
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/28/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/30/2009
            Why: the variable has been made into a string, not a boolean
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return debugRegisterState;
            }
            set
            {
                debugRegisterState = value;
            }
        }
        public string BaseRegisterState
        {
            /*!
            Procedure Name: BaseRegisterState
            Description: provides access to the BaseRegisterState string for the Instruction class
            Input Parameters: none
            Output Parameters: baseRegisterState
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/30/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return baseRegisterState;
            }
            set
            {
                baseRegisterState = value;
            }
        }
        public System.Collections.ArrayList IntermediateInstructions
        {
            /*!
            Procedure Name: IntermediateInstructions
            Description: returns the array of instructions
            Input Parameters: none
            Output Parameters: ArrayList
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/19/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/27/2009
            Why: this method needed to be turned into a property
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return this.instructionList;
            }
        }
        public DataTable SymbolTable
        {
            /*!
            Procedure Name: SymbolTable
            Description: This simply returns the symbol table.  It is a property, but it only gets the table.  
            It is needed, however, to check
            for values and to insert values into the table.  And because, by design, member variables
            are kept private, this method is needed for other classes to access the table.
            Input Parameters: none
            Output Parameters: symbolTable
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/27/2009
            Why: this method needed to be turned into a property
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return symbolTable;
            }
        }
        public DataTable InstructionsDirectivesTable
        {
            /*!
            Procedure Name: InstructionsDirectivesTable
            Description: This simply returns the combined instructions and directives table.
            Only recently did it become apparant that a combined table was needed, and this provides
            public access to it.
            Input Parameters: none
            Output Parameters: instructionsDirectivesTable
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/27/2009
            Why: this method needed to be turned into a property
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            get
            {
                return instructionDirectivesTable;
            }
        }
        public void InsertIntoErrorTable (int lineNum, int errorNum)
        {
            /*!
            Procedure Name: InsertIntoErrorTable (version 1)
            Description: This is used to add errors to the errorTable
            Input Parameters: lineNum (where the error is located in source), errorNum (the index of the error)
            Output Parameters: none
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: resultRows
            Module Where Defined: TresselIntermediate
            Data Type: DataRow[]
            Local or Global: Local
            Purpose: holds the results of the query when looking up error info
            #*/
            DataRow[] resultRows = errorTable.Select("Number=" + errorNum);
            /*#
            Variable Name: errorDescription
            Module Where Defined: TresselIntermediate
            Data Type: string
            Local or Global: Local
            Purpose: holds the error description when looking up error info
            #*/
            string errorDescription = resultRows[0][1].ToString();
            /*#
            Variable Name: errorType
            Module Where Defined: TresselIntermediate
            Data Type: string
            Local or Global: Local
            Purpose: holds the error type when looking up error info
            #*/
            string errorType = resultRows[0][2].ToString();
            /*#
            Variable Name: myDataRow
            Module Where Defined: TresselIntermediate
            Data Type: DataRow[]
            Local or Global: Local
            Purpose: the new row for the generated errors table
            #*/
            DataRow myDataRow = this.generatedErrorsTable.NewRow();
            myDataRow["Line Number"] = lineNum;
            myDataRow["Description"] = errorDescription;
            myDataRow["Type"] = errorType;

            this.generatedErrorsTable.Rows.Add(myDataRow);
        }
        public void InsertIntoErrorTable(int errorNum)
        {
            /*!
            Procedure Name: InsertIntoErrorTable (version 2)
            Description: This is used to add errors to the errorTable; it's the overloaded method which 
            doesn't require a lineNum
            Input Parameters: int errorNum (the index of the error to be inserted)
            Output Parameters: none
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: resultRows
            Module Where Defined: TresselIntermediate
            Data Type: DataRow[]
            Local or Global: Local
            Purpose: holds the results of the query when looking up error info
            #*/
            DataRow[] resultRows = errorTable.Select("Number=" + errorNum);
            /*#
            Variable Name: errorDescription
            Module Where Defined: TresselIntermediate
            Data Type: string
            Local or Global: Local
            Purpose: holds the error description when looking up error info
            #*/
            string errorDescription = resultRows[0][1].ToString();
            /*#
            Variable Name: errorType
            Module Where Defined: TresselIntermediate
            Data Type: string
            Local or Global: Local
            Purpose: holds the error type when looking up error info
            #*/
            string errorType = resultRows[0][2].ToString();
            /*#
            Variable Name: myDataRow
            Module Where Defined: TresselIntermediate
            Data Type: DataRow[]
            Local or Global: Local
            Purpose: the new row for the generated errors table
            #*/
            DataRow myDataRow = this.generatedErrorsTable.NewRow();
            myDataRow["Line Number"] = this.lineCounter;
            myDataRow["Description"] = errorDescription;
            myDataRow["Type"] = errorType;
            this.generatedErrorsTable.Rows.Add(myDataRow);
        }
        public static TresselIntermediate Parse(string tresselFile, Tressel_asm.CodeDisplayForm parentForm)
        {
            /*!
            Procedure Name: Parse
            Description: Goes through each line of input, converts into an instruction, and builds the 
            intermediate file.
            Input Parameters: string tresselFile (the next line of source)
            Output Parameters: none
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/5/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/7/2009
            Why: I just like the idea of Parse returning a complete "Program" (the one thing I liked about 
            Resolve), so I made this method static.  It then calls the constructor and returns the instance
            created.
            Who: Steve Rudy
            When: 10/28/2009
            Why: fixing Parse when dealing with code after end directive
            Who: Steve Rudy
            When: 10/31/2009
            Why: fixed error reporting when tressel source exists after end directive
            Who: Steve Rudy
            When: 11/9/2009
            Why: we needed to check and make sure the end directive was called during the program
            Who: Steve Rudy
            When: 11/11/2009
            Why: introduced source object that contains all the source lines
            Who: Steve Rudy
            When: 11/16/2009
            Why: fatal error (31) insertion block moved before line counter increment
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: tresselInstance
            Module Where Defined: TresselIntermediate
            Data Type: TresselIntermediate
            Local or Global: Local
            Purpose: represents the tressel program being assembled
            #*/
            TresselIntermediate tresselInstance = new TresselIntermediate();
            tresselInstance.codeForm = parentForm;
            tresselInstance.tresselReader = new StreamReader(tresselFile);
            /*#
            Variable Name: isRead
            Module Where Defined: TresselIntermediate
            Data Type: bool
            Local or Global: Local
            Purpose: keeps track of whether the file has reached its end or not
            #*/
            bool isRead = false;
            while (!isRead && !tresselInstance.fatalErrors)
            {
                /*#
                Variable Name: nextTresselLine
                Module Where Defined: TresselIntermediate
                Data Type: string
                Local or Global: Local
                Purpose: holds the next line read from the file
                #*/
                string nextTresselLine = tresselInstance.tresselReader.ReadLine();
                if (!(nextTresselLine == null))
                {
                    tresselInstance.source.Add(nextTresselLine);
                    if (tresselInstance.programEnds && !nextTresselLine.Trim().Equals(""))
                    {
                        tresselInstance.InsertIntoErrorTable(29);
                        tresselInstance.fatalErrors = true;
                    }
                    else if (!tresselInstance.programEnds)
                    {
                        if (!nextTresselLine.Trim().StartsWith(":") && !(nextTresselLine.Trim().Length == 0))
                        {
                            tresselInstance.currentInstruction = new Instruction(nextTresselLine, tresselInstance,true);
                            try
                            {
                                tresselInstance.currentInstruction.Parse();
                            }
                            catch (System.Exception exc)
                            {
                                System.Windows.Forms.MessageBox.Show(exc.Message + "; line: " + nextTresselLine);
                            }
                        }
                    }
                    if (tresselInstance.locationCounter > 2047)
                    {
                        tresselInstance.InsertIntoErrorTable(31);
                        tresselInstance.fatalErrors = true;
                    }
                    tresselInstance.lineCounter++;
                }
                else
                {
                    isRead = true;
                }
            }
            if (!tresselInstance.programEnds && !tresselInstance.fatalErrors)
            {
                tresselInstance.InsertIntoErrorTable(tresselInstance.lineCounter-1,49);
                tresselInstance.fatalErrors = true;
            }
            tresselInstance.tresselReader.Close();
            if (!tresselInstance.fatalErrors)
            {
                tresselInstance.GenerateLiteralsSource();
            }
            return tresselInstance;
        }
        public int LocationCounter
        {
            /*!
            Procedure Name: LocationCounter
            Description: adds public access to the locationCounter variable
            Input Parameters: none
            Output Parameters: int
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/16/2009
            Modification Log:
            Coding Standards Met:SR
            Testing Standards Met: RM
            !*/
            get
            {
                return locationCounter;
            }
            set
            {
                locationCounter = value;
            }
        }
        public bool FatalErrors
        {
            /*!
            Procedure Name: FatalErrors
            Description: adds public access to the fatalErrors variable
            Input Parameters: none
            Output Parameters: bool
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/19/2009
            Modification Log:
            Coding Standards Met:SR
            Testing Standards Met: RM
            !*/
            get
            {
                return fatalErrors;
            }
            set
            {
                fatalErrors = value;
            }
        }
        public bool InstructionAllowed
        {
            /*!
            Procedure Name: InstructionAllowed
            Description: adds public access to the instructionAllowed variable
            Input Parameters: none
            Output Parameters: bool
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/19/2009
            Modification Log:
            Coding Standards Met:SR
            Testing Standards Met: RM
            !*/
            get
            {
                return instructionAllowed;
            }
            set
            {
                instructionAllowed = value;
            }
        }
        public bool ProgramEnds
        {
            /*!
            Procedure Name: ProgramEnds
            Description: adds public access to the programEnds variable
            Input Parameters: none
            Output Parameters: bool
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/19/2009
            Modification Log:
            Coding Standards Met:SR
            Testing Standards Met: RM
            !*/
            get
            {
                return programEnds;
            }
            set
            {
                programEnds = value;
            }
        }
        private void GenerateLiteralsSource()
        {
            /*!
            Procedure Name: GenerateLiteralsSource
            Description: generates intermediate source at the end of the intermediate file for the literals in the symbol table
            Input Parameters: none
            Output Parameters: none
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/28/2009
            Modification Log:
            Who: Steve Rudy
            When: 11/2/2009
            Why: we needed to add the location for the address of the literals
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: symbolReturnRows
            Module Where Defined: GenerateLiteralsSource
            Data Type: DataRow[]
            Local or Global: Local
            Purpose: contains the rows from the symbol table that have literals
            #*/
            DataRow[] symbolReturnRows = symbolTable.Select("Usage='Literal'");
            for (int i = 0; i < symbolReturnRows.Length; i++)
            {
                /*#
                Variable Name: currentRow
                Module Where Defined: GenerateLiteralsSource
                Data Type: DataRow
                Local or Global: Local
                Purpose: holds the current row from symbolReturnRows[i]
                #*/
                DataRow currentRow = symbolReturnRows[i];
                /*#
                Variable Name: labelName
                Module Where Defined: GenerateLiteralsSource
                Data Type: string
                Local or Global: Local
                Purpose: holds the label name in the current row
                #*/
                string labelName = currentRow["Label Name"].ToString();
                /*#
                Variable Name: substitutionString
                Module Where Defined: GenerateLiteralsSource
                Data Type: string
                Local or Global: Local
                Purpose: holds the substitution string in the current row
                #*/
                string substitutionString = currentRow["Substitution String"].ToString();
                /*#
                Variable Name: dataType
                Module Where Defined: GenerateLiteralsSource
                Data Type: string
                Local or Global: Local
                Purpose: used for the opcode (WORD-NUM or WORD-CHR)
                #*/
                string dataType = "WORD-NUM";
                if (substitutionString.StartsWith("'"))
                {
                    dataType = "WORD-CHR";
                }
                /*#
                Variable Name: dataInstruction
                Module Where Defined: GenerateLiteralsSource
                Data Type: Instruction
                Local or Global: Local
                Purpose: used to add to the literal pool at the bottom of the intermediate source
                #*/
                Instruction dataInstruction = new Instruction(labelName,dataType,substitutionString,this);
                instructionList.Add(dataInstruction);
                currentRow["Address"] = locationCounter;
            }
        }
        private TresselIntermediate()
        {
            /*!
            Procedure Name: TresselIntermediate
            Description: The private constructor called the static method Parse
            Input Parameters: none
            Output Parameters: none
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/5/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/9/2009
            Why: We are no longer allowing the user to provide the location for the directives and instuctions
            file (we don't trust them), so as such we are building them ourselves and we have no need of
            calling the LoadTable method here anymore
            Who: Steve Rudy
            When: 11/11/2009
            Why: initialized the new source object
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/
            this.instructionList = new System.Collections.ArrayList();
            symbolTable = GenerateSymbolTable();
            errorTable = LoadTable(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\ErrorMessageTable.xls");
            this.instructionDirectivesTable = LoadTable(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\instructionsDirectivesTable.xls");
            this.generatedErrorsTable.Columns.Add("Line Number");
            this.generatedErrorsTable.Columns.Add("Description");
            this.generatedErrorsTable.Columns.Add("Type");
            source = new System.Collections.ArrayList();
        }
        private DataTable GenerateErrorTable()
        {
            /*!
            Procedure Name: GenerateErrorTable
            Description: creates the template for the error table
            Input Parameters: none
            Output Parameters: DataTable
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: errorDataTable
            Module Where Defined: GenerateErrorTable
            Data Type: DataTable
            Local or Global: Local
            Purpose: populated with columns then returned as the template for the error table
            #*/
            System.Data.DataTable errorDataTable = new DataTable("Error Table");
            /*#
            Variable Name: errorDataColumn
            Module Where Defined: GenerateErrorTable
            Data Type: DataColumn
            Local or Global: Local
            Purpose: used as a template to create the columns for the error table
            #*/
            DataColumn errorDataColumn;
 
            errorDataColumn = new DataColumn();
            errorDataColumn.DataType = System.Type.GetType("System.Int32");
            errorDataColumn.ColumnName = "Line Counter";
            errorDataColumn.ReadOnly = false;
            errorDataColumn.Unique = true;

            errorDataTable.Columns.Add(errorDataColumn);
     
            errorDataColumn = new DataColumn();
            errorDataColumn.DataType = System.Type.GetType("System.String");
            errorDataColumn.ColumnName = "Error Type";
            errorDataColumn.Unique = false;
            errorDataTable.Columns.Add(errorDataColumn);
 
            errorDataColumn = new DataColumn();
            errorDataColumn.DataType = System.Type.GetType("System.Int32");
            errorDataColumn.ColumnName = "Error Index";
            errorDataColumn.Unique = false;
            errorDataTable.Columns.Add(errorDataColumn);
 
            return new DataTable();
        }
        private DataTable GenerateSymbolTable()
        {
            /*!
            Procedure Name: GenerateSymbolTable
            Description: creates the template for the symbol table
            Input Parameters: none
            Output Parameters: DataTable
            Error Conditions Tested:
            Error Messages Generated:
            Original Author: Steve Rudy
            Procedure Creation Date: 10/14/2009
            Modification Log:
            Coding Standards Met: SR
            Testing Standards Met: RM
            !*/

            /*#
            Variable Name: symbolDataTable
            Module Where Defined: GenerateSymbolTable
            Data Type: DataTable
            Local or Global: Local
            Purpose: populated with columns then returned as the template for the symbol table
            #*/
            System.Data.DataTable symbolDataTable = new DataTable("Symbol Table");

            /*#
            Variable Name: myDataColumn
            Module Where Defined: GenerateSymbolTable
            Data Type: DataColumn
            Local or Global: Local
            Purpose: used as a template to create the columns for the symbol table
            #*/
            DataColumn myDataColumn;
            
            
 
            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Label Name";
            myDataColumn.ReadOnly = false;
            myDataColumn.Unique = true;

            symbolDataTable.Columns.Add(myDataColumn);
     
            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Address";
            myDataColumn.Unique = false;
            symbolDataTable.Columns.Add(myDataColumn);
 
            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Usage";
            myDataColumn.Unique = false;
            symbolDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Substitution String";
            myDataColumn.Unique = false;
            symbolDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Relocation Code";
            myDataColumn.Unique = false;
            symbolDataTable.Columns.Add(myDataColumn);
            return symbolDataTable;

        }
        

        public static string ToHex(int intToConvert)
        {
            /*!
            Procedure Name: ToHex
            Description: Converts an integer to a hexidecimal string
            Input Parameters: integer
            Output Parameters: string 
            Errors Conditions Tested:
            Error Messages Generated:
            Original Author: Steven Rudy
            Procedure Creation Date: 10/6/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/11/2009
            Why: I've moved this method from the Form class to the TresselIntermediate class, and
            to make it easier to user, I've made it static
            Coding standards met: SR
            Testing standards met: RM
            !*/

            return intToConvert.ToString("X");
        }
        public static int ToInteger(string hexToConvert)
        {
            /*!
            Procedure Name: ToInteger
            Description: Converts a hexadecimal string to an integer
            Input Parameters: string
            Output Parameters: integer
            Errors Conditions Tested:
            Error Messages Generated:
            Original Author: Ryan McDaniel
            Procedure Creation Date: 10/6/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/11/2009
            Why: I've moved this method from the Form class to the TresselIntermediate class, and
            to make it easier to user, I've made it static
            Who: Steve Rudy
            When: 11/7/2009
            Why: we needed to compensate for two's complement
            Coding standards met: SR
            Testing standards met: RM
            !*/

            /*#
            Variable Name: firstDigit
            Module Where Defined: ToInteger
            Data Type: int
            Local or Global: Local
            Purpose: keeps the first digit of hex in decimel
            #*/
            int firstDigit = int.Parse(hexToConvert[0].ToString(), System.Globalization.NumberStyles.HexNumber);
            /*#
            Variable Name: returnMultiplier
            Module Where Defined: ToInteger
            Data Type: int
            Local or Global: Local
            Purpose: return negative for error checking.
            #*/
            int returnMultiplier = 1;
            if (firstDigit > 7)
            {
                firstDigit = firstDigit - 8;
                hexToConvert.Remove(0, 1);
                hexToConvert.Insert(0, firstDigit.ToString());
                returnMultiplier = -1;
            }
            return int.Parse(hexToConvert, System.Globalization.NumberStyles.HexNumber) * returnMultiplier;
        }
        public DataTable LoadTable(string excelFile)
        {
            /*!
            Procedure Name: LoadTable
            Description: converts an excel file to a datatable
            Input Parameters: string excelFile
            Output Parameters: DataTable
            Errors Conditions Tested:
            Error Messages Generated:
            Original Author: Greg Loesch
            Procedure Creation Date: 10/13/2009
            Modification Log
            Who: Steve Rudy
            When: 10/27/2009
            Why: adding a more specific message if we are unable to locate the tables
            Coding standards met: SR
            Testing standards met: RM
            !*/

            /*#
            Variable Name: excelConnectionStr
            Module Where Defined: LoadTable
            Data Type: string
            Local or Global: Local
            Purpose: used to initialize the OleDBConnection object to connect to the excel file
            #*/
            string excelConnectionStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                + excelFile + @";Extended Properties=""Excel 8.0;HDR=YES""";
            /*#
            Variable Name: con
            Module Where Defined: LoadTable
            Data Type: OleDbConnection
            Local or Global: Local
            Purpose: used to connect to the excel file as a database
            #*/
            OleDbConnection con = new OleDbConnection(excelConnectionStr); // create a connection object
            /*#
            Variable Name: dt
            Module Where Defined: LoadTable
            Data Type: DataTable
            Local or Global: Local
            Purpose: holds the contents of the excel file
            #*/
            DataTable dt = new DataTable(); 
            con.Open();
            /*#
            Variable Name: adapter
            Module Where Defined: LoadTable
            Data Type: OleDbDataAdapter
            Local or Global: Local
            Purpose: holds the contents of the excel file before it is put into the table
            #*/
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con); 
            adapter.Fill(dt);
            con.Close(); 
            return dt;
        }
        public void InsertSymbol(String label, String address, String usage, String substitutionString, string relocationCode)
        {
            /*!
            Procedure Name: InsertSymbol
            Description: creates a new row in the symbol table
            Input Parameters: String label, String address, String usage, String substitutionString, string relocationCode
            Output Parameters: none
            Errors Conditions Tested:
            Error Messages Generated:
            Original Author: Greg Loesch
            Procedure Creation Date: 10/16/2009
            Modification Log:
            Who: Steve Rudy
            When: 10/27/2009
            Why: for coding convention, this method needed to be renamed from insertSymbol to InsertSymbol
            Coding standards met: SR
            Testing standards met: RM
            !*/

            /*#
            Variable Name: myDataRow
            Module Where Defined: insertSymbol
            Data Type: DataRow
            Local or Global: Local
            Purpose: holds the contents of the new row
            #*/
            DataRow myDataRow = this.symbolTable.NewRow();
            myDataRow["Label Name"] = label;
            myDataRow["Address"] = address;
            myDataRow["Usage"] = usage;
            myDataRow["Substitution String"] = substitutionString;
            myDataRow["Relocation Code"] = relocationCode;

            this.symbolTable.Rows.Add(myDataRow);
        }
    }
}
